<template>
  <div class="swiper-container" ref="slideshow">
    <div class="swiper-wrapper">
      <div
        class="swiper-slide"
        v-for="(slide, index) in skuImageList"
        :key="slide.id"
      >
        <img
          :src="slide.imgUrl"
          @click="higHlight(index)"
          :class="{ active: highlight == index }"
        />
      </div>
    </div>
    <div class="swiper-button-next" @click="add"></div>
    <div class="swiper-button-prev" @click="reduce"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
export default {
  name: "ImageList",
  props: ["skuImageList"],
  data() {
    return {
      highlight: 0,
    };
  },
  methods: {
    higHlight(index) {
      this.highlight = index;
      this.$bus.$emit("getIndex",this.highlight)
    },
    reduce() {
      if (this.highlight > 0) {
        this.highlight = this.highlight - 1;
      }
    },
    add() {
      if (this.highlight < this.skuImageList.length ) {
        this.highlight = this.highlight + 1;
      }
    },
  },
  watch: {
    //监听pannerList数据的变化：因为这条数据发生过变化-~~由空数组变为数组里面有四个元素
    skuImageList: {
      handler(newValue, oldValue) {
        //现在咱们通过watch监听bannerList属性的属性值的变化
        //如果执行hand1er方法，代表组件实例身上这个属性的属性以已经有了【数组：四个元素】
        //v-for执行完毕，才有结构【你现在在watch当中没办法保证的】
        //netxTick:在下次DoM更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法，获取更新后的DoM。
        this.$nextTick(() => {
          var mySwiper = new Swiper(this.$refs.slideshow, {
            // direction: "vertical", // 垂直切换选项
            // loop: true, // 循环模式选项

            // 如果需要前进后退按钮
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },

            //可以设置为数字（可为小数，小数不可loop），或者 'auto'则自动根据slides的宽度来设定数量。
            slidesPerView: "auto",
          });
        });
      },
    },
  },
};
</script>

<style lang="less" scoped>
.swiper-container {
  height: 56px;
  width: 412px;
  box-sizing: border-box;
  padding: 0 12px;

  .swiper-slide {
    width: 56px;
    height: 56px;

    img {
      width: 100%;
      height: 100%;
      border: 1px solid #ccc;
      padding: 2px;
      width: 50px;
      height: 50px;
      display: block;

      &.active {
        border: 2px solid #f60;
        padding: 1px;
      }

      // &:hover {
      //   border: 2px solid #f60;
      //   padding: 1px;
      // }
    }
  }

  .swiper-button-next {
    left: auto;
    right: 0;
  }

  .swiper-button-prev {
    left: 0;
    right: auto;
  }

  .swiper-button-next,
  .swiper-button-prev {
    box-sizing: border-box;
    width: 12px;
    height: 56px;
    background: rgb(235, 235, 235);
    border: 1px solid rgb(204, 204, 204);
    top: 0;
    margin-top: 0;
    &::after {
      font-size: 12px;
    }
  }
}
</style>